//
//  NSAttributedString+ZXCategory.h
//  YiShangbao
//
//  Created by simon on 2018/6/25.
//  Copyright © 2018年 com.Microants. All rights reserved.
//
//  可以增加很多类目便捷方法；
//  2019.10.28  增加关键字检索高亮
//  2019.11.01  增加删除线方法
//  2019.12.03 遍历数学数字
//  2020.01.21 优化
//  2020.02.07 优化遍历数字小数的方法；
//  2020.3.23  修改遍历数字小数的方法bug。
//  2020.4.28  优化方法
//  2021.02.26 插入了前缀标签附件的富文本字符串；
//  2021.03.13 增加方法，判空优化；
//  2021.06.17 标签😊：返回插入了前缀button标签附件的富文本字符串；增加判空处理，背景色处理；
//  2021.07.02 标签：修改没有用font参数的字体样式的bug；
//  2022.06.02 NSAttributedString：StyleList增加textDecoration文本样式，删除线；

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

NS_ASSUME_NONNULL_BEGIN


@interface StyleList : NSObject

@property (nonatomic, copy) NSString *color;

@property (nonatomic, assign) NSInteger size;

@property (nonatomic, copy) NSString *weight;

/// 文字外观：@"line_through"：删除线
@property (nonatomic, copy) NSString *textDecoration;

@end


@interface NSAttributedString (ZXCategory)



/// 初始化富文本字符串并添加删除线；
/// @param str 字符串
+ (NSAttributedString *)zx_attributedStringWithStrikethroughStyleWithString:(NSString *)str;


/// 初始化富文本字符串并添加下划线；
/// @param str 字符串
+ (NSAttributedString *)zx_attributedStringWithUnderlineStyleWithString:(NSString *)str;



/// 在富文本字符串中给指定range文本添加删除线，并设置删除线颜色；
/// @param range 删除线的range范围
/// @param color 删除线颜色
- (NSAttributedString *)zx_addStrikethroughTypeSingleWithRange:(NSRange)range strikethroughColor:(UIColor *)color;


//- (void)addForegroundColor:(UIColor *)color range:(NSRange)range;



/// 替换富文本字符串用指定分隔符，并用替换文本替换指定文本，赋值指定样式；
/// @param full 整个富文本字符串;例如%s%s%s;
/// @param replaceIdString 替换的字符串标识符，默认%s
/// @param targets 替换的目标文本数组;例如["应付",@"¥",@"20.0"]
/// @param styles 样式对象模型数组
+ (nullable NSMutableAttributedString *)zx_attributedStringWithAttributedString:(nullable NSAttributedString *)full
                                                       replaceIdString:(nullable NSString *)replaceIdString
                                                               targets:(NSArray *)targets
                                                                styles:(NSArray <StyleList *> *)styles;

@end



@interface NSMutableAttributedString (ZXCategory)


/// 设置NSParagraphStyle的行距，对齐方式默认为NSTextAlignmentLeft，截断模式默认为TruncatingTail；
/// @param lineSpace 行距大小
- (NSMutableAttributedString *)zx_addLineSpace:(CGFloat)lineSpace;


/// 设置NSParagraphStyle的行距，对齐方式，截断模式默认为TruncatingTail；
/// @param lineSpace lineSpace description
/// @param alignment alignment description
- (NSMutableAttributedString *)zx_addLineSpace:(CGFloat)lineSpace alignment:(NSTextAlignment)alignment;

/// 设置NSParagraphStyle的行距，对齐方式，截断模式
- (NSMutableAttributedString *)zx_addLineSpace:(CGFloat)lineSpace
                                     alignment:(NSTextAlignment)alignment
                                 lineBreakMode:(NSLineBreakMode) lineBreakMode;

/**
 检索富文本字符串的关键字，并返回字符串中给定searchString字符串每次出现的frange范围。
 @param searchString 搜索关键字
 @param block 回调
*/
/*
 NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:text];
  [attributedText zx_enumerateRangeOfString:self.searchText usingBlock:^(NSRange searchStringRange, NSUInteger idx, BOOL * _Nonnull stop) {
      
      [attributedText addAttributes:@{NSForegroundColorAttributeName:[UIColor zx_colorWithHexString:@"34373A"]} range:searchStringRange];
  }];
  cell.textLabel.attributedText = attributedText;
 */
- (void)zx_enumerateRangeOfString:(NSString *)searchString usingBlock:(void(^)(NSRange searchStringRange,NSUInteger idx, BOOL *stop))block;


/// 遍历数学数字,以小数点为分割获取小数点前整数的stringRange，小数点后数字的range；例如10.55;如果小数点前置或后置searchString没有找到或为空("”),不返回{NSNotFound, 0}; //2020.3.23 修改bug。
/// @param prefixBlock 小数点前置的字符串Range的block;
/// @param suffixBlock 小数点后置的range；
///  例如：
///  NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] initWithString:model.todaySalesVolume];
///  [attString zx_enumerateDecimalNumberWithDotSeperateUsingDotPrefixBlock:nil dotSuffixBlock:^(NSRange dotSuffixRange) {
///              [attString addAttributes:@{NSFontAttributeName: [UIFont zx_systemFontOfSize:32 weight:UIFontWeightMedium]} range:NSMakeRange(dotSuffixRange.location-1, dotSuffixRange.length+1)];
///}];
- (void)zx_enumerateDecimalNumberWithDotSeperateUsingDotPrefixBlock:(void(^)(NSRange dotPrefixRange))prefixBlock dotSuffixBlock:(void(^)(NSRange dotSuffixRange))suffixBlock;




/// 2021.2.26 返回插入了前缀label标签附件的富文本字符串；
/// @param title 标签文本
/// @param textColor 标签文本颜色，[UIColor blackColor]
/// @param titleFont 标签文本字体
/// @param contentInset 标签文本内边距，默认UIEdgeInsetsMake(1, 2, 1, 2)
/// @param radius 标签圆角
/// @param borderWidth 标签圆角边框宽度
/// @param borderColor 圆角边框颜色
/// @param backgroundColor 标签背景颜色，默认[UIColor whiteColor]
/// 例如:反向遍历插入
/// NSArray *lables = [NSArray yy_modelArrayWithClass:[ZXLabelsTitleWithColorModel class] json:recommendItem.labels];
/// NSString *titleString = [NSString stringWithFormat:@"%@",recommendItem.spuName];
/// NSMutableAttributedString *mutableTitleString = [[NSMutableAttributedString alloc] initWithString:titleString];
/// [lables enumerateObjectsWithOptions:NSEnumerationReverse usingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
///    ZXLabelsTitleWithColorModel *lableModel = (ZXLabelsTitleWithColorModel *)obj;
///    UIColor *color = [UIColor zx_colorWithHexString:lableModel.hexColor];
 ///   [mutableTitleString zx_insetPrefixAttachmentLabelWithLabelTitle:lableModel.text textColor:color titleFont:[UIFont zx_systemFontOfSize:11] contentInset:UIEdgeInsetsZero labelCornerRadius:2 borderWidth:1 borderColor:color];
///}];
///self.nameLabel.attributedText = mutableTitleString;

- (void)zx_insetPrefixAttachmentLabelWithLabelTitle:(NSString *)title
                                          textColor:(nullable UIColor *)textColor
                                          titleFont:(nullable UIFont *)titleFont
                                       contentInset:(UIEdgeInsets)contentInset
                                  labelCornerRadius:(CGFloat)radius
                                        borderWidth:(CGFloat)borderWidth
                                        borderColor:(nullable UIColor *)borderColor
                                    backgroundColor:(nullable UIColor *)backgroundColor;


/// 2021.6.17 返回插入了前缀button标签附件的富文本字符串；
- (void)zx_insetPrefixAttachmentButtonWithIconImage:(UIImage *)iconImage
                                         labelTitle:(NSString *)title
                                          textColor:(nullable UIColor *)textColor
                                          titleFont:(nullable UIFont *)titleFont
                                       contentInset:(UIEdgeInsets)contentInset
                                  labelCornerRadius:(CGFloat)radius
                                        borderWidth:(CGFloat)borderWidth
                                        borderColor:(nullable UIColor *)borderColor
                                    backgroundColor:(nullable UIColor *)backgroundColor;





/// 初始化一个¥10，¥1.24格式的价格富文本字符串，使用NSDecimalNumber技术自动忽略尾号的0；
/// @param amountString 价格字符串
/// @param symbolAttrs “¥”符号属性
/// @param priceAttrs 价格字符串属性
+ (NSMutableAttributedString *)zx_attributedStringWithPrefixCNYString:(NSString *)amountString symbolAttributes:(NSDictionary<NSAttributedStringKey, id> * _Nullable)symbolAttrs priceAttributes:(NSDictionary<NSAttributedStringKey, id> * _Nullable)priceAttrs;



@end

NS_ASSUME_NONNULL_END
